package main

import (
	"fmt"
	"net"
	"os/exec"
)

func cgi_execute (con *net.TCPConn, stat header, content string) {
	var res int
	var err error
	var cmd *exec.Cmd

	if (stat.method == post) {
		cmd = exec.Command (stat.url, content)
	} else {
		cmd = exec.Command (stat.url, stat.query)
	}

	out, err := cmd.StdoutPipe ()
	if (err != nil) {
		fmt.Println ("Get stdout error, ", err)
		return
	}

	cmd.Start ()
	var data [1024]byte

	response_ok (con)
	for {
		res, err = out.Read (data[:])
		if (res == 0 || err != nil) {
			break
		}
		res, err = con.Write (data[:res])
		if (res == 0 || err != nil) {
			fmt.Println (res)
			fmt.Println (err)
			break
		}
	}
	cmd.Wait ()
}
